package demo2;

import java.util.Arrays;
import java.util.Stack;

public class Mystack {
    private int[] data;
    private int usedSize;

    public Mystack() {
        this.data = new int[10];
        usedSize = 0;
    }

    public void push(int val) {
        if(isFull()) {
            data = Arrays.copyOf(data, 2*data.length);
        }
        data[usedSize] = val;
        usedSize++;
    }

    public boolean isFull() {
        return usedSize == data.length;
    }

    public int pop() {
        //判断栈是否为空
        if(isEmpty()) {
            throw new StackEmptyException("Stack is empty!");
        }
        //弹出元素
        int val = data[usedSize-1];
        usedSize--;
        return val;
        //return data[--usedSize];
    }

    public int peek() {
        //判断栈是否为空
        if(isEmpty()) {
            throw new StackEmptyException("Stack is empty!");
        }
        return data[usedSize-1];
    }

    public boolean isEmpty() {
        return usedSize == 0;
    }

}
